<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-row>
        <el-col :span="6">
          <el-button type="primary" size="small" @click="checkInbound">入库确认</el-button>
          <el-button type="primary" size="small" @click="dialogVisible=true">扫码入库</el-button>
        </el-col>
        <el-col :span="18" style="text-align: right">
          <el-input size="small" style="width: 200px" class="filter-item more-search" placeholder="订单号-回车查询"
                    v-model="search.condition.key" @keyup.enter.native.prevent="list"></el-input>
          <!--搜索-->
          <el-button class="filter-item" size="small" type="primary" v-waves icon="el-icon-circle-plus-outline"
                     @click="moreClick">更多
          </el-button>
          <el-dropdown trigger="click" :hide-on-click=false style="float: right;margin-left: 10px">
            <span class="el-dropdown-link">
              <el-button class="filter-item" size="small" type="primary" v-waves>
                选择列
                <i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-checkbox-group v-model="columnCheckList">
                  <el-checkbox label="id">通知单号</el-checkbox>
                  <el-checkbox label="storeHouseName">仓库</el-checkbox>
                  <el-checkbox label="ownerId">客户</el-checkbox>
                  <el-checkbox label="ownerOrderNo">客户订单号</el-checkbox>
                  <el-checkbox label="status">入库状态</el-checkbox>
                  <el-checkbox label="inspectStatus">质检状态</el-checkbox>
                  <el-checkbox label="materielId">车型名称</el-checkbox>
                  <el-checkbox label="lotNo1">车架号</el-checkbox>
                  <el-checkbox label="materielName">车型描述</el-checkbox>
                </el-checkbox-group>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
      <div style="clean:both"></div>
      <!--更多内容-->
      <div :class="{'search-transition':moreSearchVisible,h0:true,h46:moreSearchVisible,'search-content':true}">
        <el-form ref="form" :model="search.condition" label-width="100px" :inline="true">
          <el-form-item label="单据状态">
            <el-select v-model="search.condition.status" placeholder="请选择单据状态">
              <el-option label="未选择" value=""></el-option>
              <el-option label="未入库" value="10"></el-option>
              <el-option label="部分入库" value="20"></el-option>
              <el-option label="全部入库" value="30"></el-option>
              <el-option label="取消入库" value="40"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="车架号">
            <el-input placeholder="请输入车架号" class="w200" clearable v-model="search.condition.lotNo1"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="list" style="margin-left: 10px">查询</el-button>
            <el-button type="primary" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!--列表-->
    <el-table ref="stockInitTable" :data="stockInitList" @selection-change="handleSelectionChange"
              v-loading="listLoading" element-loading-text="加载中" border fit highlight-current-row
              @row-click="rowClick"
              style="width: 100%;margin-top:5px">
      <el-table-column type="selection" width="35"></el-table-column>
      <el-table-column :key="key" align="center" :width="value.width" v-for='(value,key) in columnList'
                       :label="value.title">
        <template slot-scope="scope">
          <div v-if="value.key === 'status'">{{scope.row[value.key] | inboundLineStatusFilter}}</div>
          <div v-else-if="value.key === 'inspectStatus'">{{scope.row[value.key] | inboundInspectStatusFilter}}</div>
          <div v-else>{{scope.row[value.key]}}</div>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination :current-page.sync="page.pageNo" :page-sizes="[10,20,30,50]" @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :page-size="page.pageSize" :total="page.totalRecord"
                     layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    </div>
    <el-dialog title="入库确认" width="50%" :visible.sync="dialogVisible" @close="dialogVisibleClose">
      <hr>
      <el-row>
        <el-col style="width: 120px;text-align: right;margin-right: 10px;line-height: 32px;height: 32px;">车架号(vin)
        </el-col>
        <el-input placeholder="vin码-回车查询" v-model="columnDetailDialogList.lotNo1in"
                  style="margin-right: 10px;width: 200px" clearable @keyup.enter.native="searchDetail"/>
        <span>
          <el-button type="primary" @click="taskPutWay"
                     :disabled="columnDetailDialogList.status === '30'">入库确认</el-button>
          <el-button type="primary" :disabled="!(columnDetailDialogList.lotNo1)" @click="printQR">打印二维码</el-button>
        </span>
      </el-row>
      <hr>
      <el-form ref="inboundForm" :model="columnDetailDialogList" label-width="120px" :inline="true">
        <el-form-item label="订单号: ">
          <div class="w200 printBig">{{columnDetailDialogList.ownerOrderNo}}</div>
        </el-form-item>
        <el-form-item label="VIN: ">
          <div class="w200 printBig">{{columnDetailDialogList.lotNo1}}</div>
        </el-form-item>
        <el-form-item label="车型:">
          <div class="w200 printBig">{{columnDetailDialogList.materielName}}</div>
        </el-form-item>
        <el-form-item label="货主: ">
          <div class="w200 printBig">{{columnDetailDialogList.ownerId}}</div>
        </el-form-item>
        <el-form-item label="收货仓库: ">
          <div class="w200 printBig">{{columnDetailDialogList.storeHouseName}}</div>
        </el-form-item>
        <el-form-item label="入库时间: ">
          <div class="w200 printBig">{{columnDetailDialogList.gmtCreate}}</div>
        </el-form-item>
        <el-form-item label="目的地: ">
          <div class="w200 printBig">{{columnDetailDialogList.destLocationDetail}}</div>
        </el-form-item>
        <div v-show="columnDetailDialogList.status === '30'">
          <el-form-item label="库区">
            <div class="w200 printBig">{{columnDetailDialogList.storeAreaName}}</div>
          </el-form-item>
          <el-form-item label="库位">
            <div class="w200 printBig">{{columnDetailDialogList.locationCode}}</div>
          </el-form-item>
        </div>
        <div v-show="columnDetailDialogList.status !== '30'">
          <el-form-item label="库区">
            <el-select class="w200" v-model="columnDetailDialogList.storeAreaId" @change="areasChange"
                       placeholder="请选择库区">
              <el-option v-for="forArea in areas" :label="forArea.storeAreaName" :value="forArea.storeAreaId"
                         :key="forArea.storeAreaId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="库位">
            <el-select class="w200" v-model="columnDetailDialogList.locationId" placeholder="请选择库位">
              <el-option v-for="forLoaction in storeLocationList" :label="forLoaction.name" :value="forLoaction.id"
                         :key="forLoaction.id"></el-option>
            </el-select>
          </el-form-item>
        </div>
      </el-form>
      <el-dialog title="打印二维码" width="500px" :visible.sync="printQRFlag" append-to-body>
        <hr>
        <div id="printQRBox">
          <el-row>
            <el-col :span="8">
              <div class="print-QR-img">
                <img :src="img_src_QR" class="img_src_QR" alt="">
              </div>
            </el-col>
            <el-col :span="16">
              <el-row class="print-QR-row">
                <el-col>
                  <span>底盘号：</span>
                  <span>{{columnDetailDialogList.lotNo1}}</span>
                </el-col>
              </el-row>
              <el-row class="print-QR-row">
                <el-col>
                  <span>库位号：</span>
                  <span>{{getStoreArea(columnDetailDialogList.storeAreaId)}}</span>
                  <span>{{getLocationName(columnDetailDialogList.storeAreaId, columnDetailDialogList.locationId)}}</span>
                </el-col>
              </el-row>
              <el-row>
                <el-col>
                  <span>运单号：</span>
                  <span>{{columnDetailDialogList.ownerOrderNo}}</span>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </div>
        <el-row>
          <el-col style="margin-left: 340px">
            <el-button type="primary" @click="printQRConfirm">打印</el-button>
            <el-button type="primary" @click="printQRCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </el-dialog>
  </div>
</template>
<script>
  import * as inbounAPI from '@/api/inbound/inboundPutaway'
  import waves from '@/directive/waves' // 水波纹指令
  const defaultColumn = [
    { key: 'id', title: '通知单号', width: '' },
    { key: 'storeHouseName', title: '仓库', width: '' },
    { key: 'ownerId', title: '客户', width: '' },
    { key: 'ownerOrderNo', title: '客户订单号', width: '' },
    { key: 'status', title: '入库状态', width: '' },
    { key: 'inspectStatus', title: '质检状态', width: '' },
    { key: 'materielId', title: '车型名称', width: '' },
    { key: 'lotNo1', title: '车架号', width: '' },
    { key: 'materielName', title: '车型描述', width: '' }
  ]
  export default {
    directives: {
      waves
    },
    data() {
      return {
        search: {
          condition: {
            lotNo1: null,
            status: null,
            key: null
          }
        },
        multipleSelection: [],
        columnList: defaultColumn,
        columnCheckList: [
          'id',
          'storeHouseName',
          'ownerId',
          'ownerOrderNo',
          'status',
          'inspectStatus',
          'materielId',
          'lotNo1',
          'materielName'
        ],
        columnDetailDialogList: {
          storeAreaId: '',
          locationId: ''
        },
        page: {
          pageNo: 1,
          pageSize: 10,
          totalRecord: 0
        },
        areas: [{ storeLocationList: [] }],
        storeLocationList: [],
        loactions: [],
        dialogVisible: false,
        moreSearchVisible: false,
        stockInitList: [],
        detailDialogList: [],
        listLoading: false,
        printQRFlag: false,
        temp: {
          id: 1
        },
        img_src_QR: '',
        imgBase64: ''
      }
    },
    created() {
      this.list()
      this.queryUsableLocation()
    },
    watch: {
      columnCheckList(valArr) {
        this.columnList = defaultColumn.filter(v => {
          for (let i = 0; i < valArr.length; i++) {
            if (v.key === valArr[i]) {
              return v
            }
          }
        })
      }
    },
    methods: {
      // 行选中
      rowClick(row, event, column) {
        // 标记当前选中行
        // this.$refs.stockInitTable.clearSelection()
        // this.$refs.stockInitTable.toggleRowSelection(row)
      },
      // 打印二维码
      printQR() {
        inbounAPI.printBarcode({
          key: this.columnDetailDialogList.lotNo1,
          houseId: this.$store.getters.currentStorehouse.id
        }).then(res => {
          if (res.code === 0) {
            this.img_src_QR = 'data:image/png;base64,' + res.data
            this.imgBase64 = res.data
          }
        })
        this.printQRFlag = true
      },
      // 确认打印
      printQRConfirm() {
        const that = this
        const localNo = that.getStoreArea(that.columnDetailDialogList.storeAreaId) + ' ' +
                        that.getLocationName(that.columnDetailDialogList.storeAreaId, that.columnDetailDialogList.locationId)
        if (this.tools.checkPrintInstall()) {
          // this.tools.printHTML(document.getElementById('printQRBox').innerHTML, 790, 1)
          this.tools.printQrImp(that.imgBase64, that.columnDetailDialogList.lotNo1, localNo, that.columnDetailDialogList.ownerOrderNo)
        }
      },
      // 取消打印
      printQRCancel() {
        this.printQRFlag = false
      },
      dialogVisibleClose() {
        this.columnDetailDialogList = {
          storeAreaId: '',
          locationId: ''
        }
      },
      // 获取库位名字
      getStoreArea(id) {
        if (id !== '' && id === null) {
          const t = this.areas.filter(i => i.storeAreaId === id)[0]
          if (t) {
            return t.storeAreaName
          } else {
            return ''
          }
        }
        return ''
      },
      getLocationName(id, id1) {
        if (id && id1) {
          const t = this.areas.filter(i => i.storeAreaId === id)[0]
          const s = t.storeLocationList.filter(i => i.id === id1)
          return s[0].name
        }
        return ''
      },
      // 重置
      reset() {
        this.search.condition = {
          lotNo1: null,
          status: null,
          key: null
        }
        this.list()
      },
      list() {
        const that = this
        // 更多
        const queryParam = {}
        if (this.moreSearchVisible) {
          queryParam.condition = that.search.condition
        } else {
          queryParam.condition = {
            key: that.search.condition.key
          }
        }
        // 分页
        queryParam.current = this.page.pageNo
        queryParam.size = this.page.pageSize
        queryParam.condition.houseId = this.$store.getters.currentStorehouse.id
        this.listLoading = true
        inbounAPI.inboundTaskQueryPage(queryParam).then(result => {
          if (result.code === 0) {
            this.stockInitList = result.data.records
            this.page.pageNo = result.data.current
            this.page.pageSize = result.data.size
            this.page.totalRecord = result.data.total
            that.listLoading = false
          }
        })
      },
      checkInbound() {
        this.$confirm('确认对选中项目入库?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.inboundPutAwayConfirm()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作',
            duration: 2000
          })
        })
      },
      inboundPutAwayConfirm() {
        const queryParam = {}
        const ids = []
        const that = this
        this.multipleSelection.forEach(row => {
          ids.push(row.id)
        })
        queryParam.lineIds = ids
        queryParam.houseId = this.$store.getters.currentStorehouse.id
        this.listLoading = true
        inbounAPI.inboundTaskPutAwayList(queryParam).then(result => {
          that.listLoading = false
          if (result.code === 0) {
            this.$message({
              message: result.message,
              type: 'info',
              duration: 2000
            })
            this.list()
          }
        }).catch(() => {
          that.listLoading = false
        })
      },
      searchDetail() {
        const queryParam = {}
        queryParam.key = this.columnDetailDialogList.lotNo1in
        queryParam.houseId = this.$store.getters.currentStorehouse.id
        inbounAPI.inboundTaskSeachDetail(queryParam).then(result => {
          if (result.code === 0) {
            this.columnDetailDialogList = result.data
            this.columnDetailDialogList.lotNo1in = queryParam.key
            this.queryUsableLocation()
          } else if (result.code === -1) {
            //
          } else {
            this.$notify({
              title: '信息提示',
              message: result.message,
              type: 'error',
              duration: 2000
            })
          }
        }).catch(() => {
          this.columnDetailDialogList = {
            storeAreaId: '',
            locationId: ''
          }
        })
      },
      queryUsableLocation() {
        const queryParam = {}
        queryParam.houseId = this.$store.getters.currentStorehouse.id
        inbounAPI.queryUsableLocation(queryParam).then(result => {
          if (result.code === 0) {
            this.storeLocationList = result.data[0].storeLocationList
            this.columnDetailDialogList.storeAreaId = result.data[0].storeAreaId
            this.columnDetailDialogList.locationId = result.data[0].storeLocationList[0].id
            this.areas = result.data
          } else {
            this.$notify({
              title: '信息提示',
              message: result.message,
              type: 'error',
              duration: 2000
            })
          }
        })
      },
      // 确认入库
      taskPutWay(val) {
        const queryParam = this.columnDetailDialogList
        queryParam.houseId = this.$store.getters.currentStorehouse.id
        queryParam.locationId = this.columnDetailDialogList.locationId
        queryParam.storeAreaId = this.columnDetailDialogList.storeAreaId
        inbounAPI.inboundTaskPutWay(queryParam).then(result => {
          if (result.code === 0) {
            this.$notify({
              title: '信息提示',
              message: '操作成功',
              type: 'success'
            })
            this.list()
          } else {
            this.$notify({
              title: '信息提示',
              message: result.message,
              type: 'error',
              duration: 2000
            })
          }
        })
      },
      // 展开更多
      moreClick() {
        this.moreSearchVisible = !this.moreSearchVisible
      },
      handleSizeChange(val) {
        // 分页size变化
        this.page.pageSize = val
        this.list()
      },
      // // 选中绑定列
      // toggleSelection(rows) {
      //   if (rows) {
      //     rows.forEach(row => {
      //       this.$refs.multipleTable.toggleRowSelection(row)
      //     })
      //   } else {
      //     this.$refs.multipleTable.clearSelection()
      //   }
      // },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      handleCurrentChange(val) {
        // 分页当前页变化
        this.page.pageNo = val
        this.list()
      },
      areasChange(val) {
        const areaid = this.columnDetailDialogList.storeAreaId
        for (let i = 0; i < this.areas.length; i++) {
          if (this.areas[i].storeAreaId === areaid) {
            this.loactions = this.areas[i].storeLocationList
            this.storeLocationList = this.areas[i].storeLocationList
            break
          } else {
            this.loactions = []
            this.storeLocationList = []
          }
        }
        // 过滤路径
        const t = this.areas.filter(i => i.storeAreaId === val)
        this.storeLocationList = t[0].storeLocationList
        this.columnDetailDialogList.locationId = t[0].storeLocationList[0].id
      }
    }
  }
</script>
<style type="text/scss" lang="scss" scoped>
  .print-QR-img{
    width: 160px;
    height: 170px;
    position: relative;
    overflow: hidden;
    .img_src_QR{
      position: absolute;
      width: 100%;
      left: -14px;
      top: -14px;
    }
  }
  .print-QR-row{
    margin-bottom: 34px;
  }
  .printBig{
    font-size: 14px;
    font-weight: bold;
  }
</style>
